﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Collections.Specialized;

namespace Yuanlin.Business
{
    public class BusinessInfoEditControl : YuanlinUserControl,
        IReplyConverter<BusinessInfoReply, GetOperation>
    {
        protected ValuedDropDownList categoryDropDownList;
        protected ValuedTextBox titleValuedTextBox;
        protected ValuedTextBox numValuedTextBox;
        protected ValuedTextBox visitGreenNumValuedTextBox;
        protected ValuedTextBox createDateValuedTextBox;

        protected IButton SaveButton;
        protected IButton BackButton;
        private BusinessInfo businessInfo;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetBusinessInfoRequest(this));
        }
        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        private void Bind()
        {
            if(businessInfo!=null)
            {
                this.titleValuedTextBox.Value = businessInfo.Title.Value;
                this.numValuedTextBox.Value = businessInfo.Num.Value.ToString();
                this.visitGreenNumValuedTextBox.Value = businessInfo.VisitGreenNum.Value;
                this.createDateValuedTextBox.Value = businessInfo.CreateDate.Value.ToShortDateString();
                BindDDL();
                categoryDropDownList.SelectedValue = businessInfo.CategoryId.Value.ToString();
            }   
        }
        private void BindDDL()
        {
            HybridDictionary hd = new HybridDictionary();
            hd.Add((int)CategoryType.CompanyProduct, "花木");
            hd.Add((int)CategoryType.ZiCaiInfo, "资材");
            categoryDropDownList.DataSource = hd;
            categoryDropDownList.DataValueField = "key";
            categoryDropDownList.DataTextField = "value";
            categoryDropDownList.DataBind();
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            if (businessInfo != null)
            {
                businessInfo.Title.Value = CensorWordManager.CensorWord(titleValuedTextBox.Value);
                businessInfo.CategoryId.Value = Int32.Parse(categoryDropDownList.SelectedValue);
                businessInfo.ProductId.Value = GetEntityId(businessInfo.Title.Value, businessInfo.CategoryId.Value.ToString());
                businessInfo.Num.Value = Int32.Parse(this.numValuedTextBox.Value);
                businessInfo.VisitGreenNum.Value = CensorWordManager.CensorWord(this.visitGreenNumValuedTextBox.Value);
                if (createDateValuedTextBox.Text.Trim().Length == 0)
                {
                    businessInfo.CreateDate.Value = DateTime.Now;
                }
                else
                {
                    businessInfo.CreateDate.Value = DateTime.Parse(createDateValuedTextBox.Text);
                }

                BusinessManager.UpdateBusinessInfo(businessInfo);
            }
            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetBusinessInfoListUrl());
        }
        /// <summary>
        /// 通过类别和名称得到具体的EntityId
        /// </summary>
        /// <param name="title">名称（如常用名）</param>
        /// <param name="categoryId">如花木或资材</param>
        /// <returns>得到 公司花木或资材的 EntityId</returns>
        private int GetEntityId(string title, string categoryId)
        {
            int entityId = 0;

            if (categoryDropDownList.SelectedValue == ((int)CategoryType.CompanyProduct).ToString())
            {
                CompanyProductListRequest request = new CompanyProductListRequest();
                request.Title = title;
                EntityList companyProducts = BusinessManager.GetCompanyProductList(RequestHelper.BuildGetCompanyProductListRequest(request));
                if (companyProducts.Count > 0)
                {
                    entityId = (companyProducts[0] as CompanyProduct).EntityId.Value;
                }
            }
            else if (categoryDropDownList.SelectedValue == ((int)CategoryType.ZiCaiInfo).ToString())
            {
                ZiCaiInfoListRequest request = new ZiCaiInfoListRequest();
                request.Title = title;
                EntityList ziCaiInfos = BusinessManager.GetZiCaiInfoList(RequestHelper.BuildGetZiCaiInfoListRequest(request));
                if (ziCaiInfos.Count > 0)
                {
                    entityId = (ziCaiInfos[0] as ZiCaiInfo).EntityId.Value;
                }
            }

            return entityId;
        }

        #region IReplyConverter<BusinessInfoReply,GetOperation> Members

        public void ConvertReply(BusinessInfoReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                businessInfo = reply.Entity as BusinessInfo;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}